from flask import Blueprint, render_template, request, jsonify,session,redirect
from app.models.UserModel import UserModel
from app import app
from app.services import *
from app.models.TaskModel import TaskModel

home = Blueprint('home', __name__)

@home.route('/')
def index_home():
    userInfo = session.get('userinfo',None)
    from_url = request.base_url
    if not UserAuth.isLogin():return redirect('/user/login?from={url}'.format(url=from_url))
    if session.get('userinfo', {}).get('isadmin', False):
        isadmin = '(管理员)'
    else:
        isadmin = ''
    return render_template('index.html',username=userInfo.get('username',None),isadmin=isadmin)


@home.route('/c')
def test_123():
    action = request.args.get('action', None)
    if action == 'del':
        res = UserModel.deleteUser({UserModel.username == 'cbw'})
    elif action == 'all':
        page = int(request.args.get('page',1))
        limit = int(request.args.get('limit',20))
        res = TaskModel.getPageList({},page=page,limit=limit)
    elif action == 'find':
        res = TaskModel().getAll(TaskModel,None)
    elif action == 'send':
        res = TaskMessage.created('haha')
    else:
        res = None
    return jsonify({'status': res})
